home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_gnome-pilot.idb / usr / freeware / share / gob / gnome-pilot-client.gob.z / gnome-pilot-client.gob
Text File  |  2001-10-09  |  29KB  |  977 lines

  1. /* gnome-pilot-client.gob
  2.  *
  3.  * Copyright (C) 1999-2000 Free Software Foundation
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the
  17.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18.  * Boston, MA 02111-1307, USA.
  19.  *
  20.  * Authors: Eskil Heyn Olsen <eskil@eazel.com>
  21.  *
  22.  */
  23.  
  24. /*
  25.  
  26.   Usage:
  27.   Most calls return 
  28.   GPILOTD_ERR_INVAL          on invalid arguments
  29.   GPILOTD_ERR_NOT_CONNECTED  connection to the gpilotd is not established
  30.   GPILOTD_ERR_INTERNAL       on internal fatal object gruesome cruel cursed state
  31.   GPILOTD_ERR_FAILED         on failed request
  32.  
  33.   ( in general (return val <= 0) == things went wrong )
  34.  
  35.  */
  36.  
  37. %h{
  38. #include <config.h>
  39. #include <stdio.h>
  40. #include <gtk/gtkobject.h>
  41. #ifdef USING_OAF
  42. #include <liboaf/liboaf.h>
  43. #else /* USING_OAF */
  44. #include <libgnorba/gnorba.h>
  45. #endif /* USING_OAF */
  46. #include <gpilotd/gnome-pilot.h>
  47.  
  48. enum GPilotdErrCode {
  49.     GPILOTD_OK=0,
  50.     GPILOTD_ERR_INVAL=-1,
  51.     GPILOTD_ERR_NOT_CONNECTED=-2,
  52.     GPILOTD_ERR_FAILED=-3,
  53.     GPILOTD_ERR_INTERNAL=-4
  54. };
  55. %}
  56.  
  57. %{
  58.  
  59. #include <sys/types.h>
  60. #include <signal.h>
  61.     
  62. typedef struct {
  63.     POA_GNOME_Pilot_Client servant;
  64.     GnomePilotClient *self;
  65. } GnomePilotClientServant;
  66.  
  67. GnomePilotClient* get_self(PortableServer_Servant servant);
  68. pid_t gpilotd_get_pid(void);
  69.  
  70. PortableServer_ServantBase__epv base_epv = {
  71.     NULL,
  72.     NULL,
  73.     NULL
  74. };
  75.  
  76. static POA_GNOME_Pilot_Client__epv  gnome_pilot_client_epv;
  77. static POA_GNOME_Pilot_Client__vepv gnome_pilot_client_vepv = { &base_epv, &gnome_pilot_client_epv };
  78.  
  79. enum get_triple_ptr_action {
  80.     GPILOTD_APP_PILOT_NAMES,
  81.     GPILOTD_APP_PILOTS_BY_NAME,
  82.     GPILOTD_APP_PILOTS_BY_LOGIN,
  83.     GPILOTD_APP_USER_NAMES,
  84.     GPILOTD_APP_DATABASES_FROM_CACHE,
  85.     GPILOTD_APP_CRADLES
  86. };
  87.  
  88. %}
  89.  
  90. class Gnome:Pilot:Client from Gtk:Object {
  91.     public GNOME_Pilot_Daemon gpilotddaemon;
  92.         public GNOME_Pilot_Client gpilotdclient;
  93.     public CORBA_Environment ev;
  94.     public CORBA_char *client_id;
  95.     public CORBA_ORB orb;
  96.  
  97.     private void init_corba_class(self) {
  98.         static gboolean inited = FALSE;
  99.         
  100.         if (inited==FALSE) {
  101.             self->client_id = NULL;
  102.             inited = TRUE;
  103.             gnome_pilot_client_epv._get_client_id = corba_get_client_id;
  104.             gnome_pilot_client_epv._set_client_id = corba_set_client_id;
  105.             gnome_pilot_client_epv.connect = corba_connect;
  106.             gnome_pilot_client_epv.disconnect = corba_disconnect;
  107.             gnome_pilot_client_epv.request_completed = corba_request_completed;
  108.             gnome_pilot_client_epv.userinfo_requested = corba_user_info_requested;
  109.             gnome_pilot_client_epv.sysinfo_requested = corba_system_info_requested;
  110.             gnome_pilot_client_epv.conduit_start = corba_conduit_start;
  111.             gnome_pilot_client_epv.conduit_end = corba_conduit_end;
  112.             gnome_pilot_client_epv.conduit_progress = corba_conduit_progress;
  113.             gnome_pilot_client_epv.conduit_message = corba_conduit_message;
  114.             gnome_pilot_client_epv.conduit_error = corba_conduit_error;
  115.         }
  116.     }
  117.  
  118.     public GnomePilotClient* 
  119.     new(void) {
  120. #if USING_OAF
  121.             int argc=0;
  122.             char *argv=NULL;
  123. #endif /* USING_OAF */
  124.             GtkObject *ret;
  125.             GnomePilotClient *obj;
  126.             CORBA_Environment *ev;
  127.             PortableServer_POA the_poa;
  128.             GnomePilotClientServant *gnome_pilot_client_servant;
  129.  
  130.             ret  = GTK_OBJECT(GET_NEW);
  131.             obj = SELF(ret);
  132.  
  133.             ev = &SELF(ret)->ev;
  134.             init_corba_class (SELF (ret));
  135.  
  136.             CORBA_exception_init (ev);
  137. #if USING_OAF
  138.             SELF(ret)->orb = oaf_init (argc, &argv);
  139. #else /* USING_OAF */
  140.             SELF(ret)->orb = gnome_CORBA_ORB ();
  141. #endif /* USING_OAF */
  142.  
  143.             g_assert (SELF(ret)->orb!=NULL);
  144.                     
  145.             gnome_pilot_client_servant = g_new0(GnomePilotClientServant,1);
  146.             gnome_pilot_client_servant->servant.vepv = 
  147.                                         &gnome_pilot_client_vepv;
  148.             gnome_pilot_client_servant->self = SELF(ret);
  149.             
  150.             the_poa = (PortableServer_POA)CORBA_ORB_resolve_initial_references(SELF(ret)->orb,"RootPOA",ev);
  151.             if (ev->_major != CORBA_NO_EXCEPTION) {
  152.                 g_warning ("%s:%d: Caught exception: %s",
  153.                        __FILE__,__LINE__, 
  154.                        CORBA_exception_id (ev));
  155.                 CORBA_exception_free(ev);
  156.             }
  157.  
  158.             PortableServer_POAManager_activate(PortableServer_POA__get_the_POAManager(the_poa, ev), ev);
  159.             if (ev->_major != CORBA_NO_EXCEPTION) {
  160.                 g_warning ("%s:%d: Caught exception: %s",
  161.                        __FILE__,__LINE__, 
  162.                        CORBA_exception_id (ev));
  163.                 CORBA_exception_free(ev);
  164.             }
  165.  
  166.             POA_GNOME_Pilot_Client__init((PortableServer_Servant)gnome_pilot_client_servant,ev);
  167.             if (ev->_major != CORBA_NO_EXCEPTION) {
  168.                 g_warning ("%s:%d: Caught exception: %s",
  169.                        __FILE__,__LINE__, 
  170.                        CORBA_exception_id (ev));
  171.                 CORBA_exception_free(ev);
  172.             }
  173.  
  174.             CORBA_free(
  175.                 PortableServer_POA_activate_object(the_poa,
  176.                                    gnome_pilot_client_servant,
  177.                                    ev)
  178.                 );
  179.             if (ev->_major != CORBA_NO_EXCEPTION) {
  180.                 g_warning ("%s:%d: Caught exception: %s",
  181.                        __FILE__,__LINE__, 
  182.                        CORBA_exception_id (ev));
  183.                 CORBA_exception_free(ev);
  184.             }
  185.             
  186.             SELF(ret)->gpilotdclient = 
  187.                 PortableServer_POA_servant_to_reference(the_poa, 
  188.                                     gnome_pilot_client_servant, 
  189.                                     ev);
  190.             if (ev->_major != CORBA_NO_EXCEPTION) {
  191.                 g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (ev));
  192.                 CORBA_exception_free(ev);
  193.             }            
  194.             
  195.             return obj;
  196.     }
  197.  
  198.         public void destroy(self) {
  199.         g_free(self->client_id);
  200.         /* FIXME: ick, what more to do here ? */
  201.         }
  202.     
  203.         public gint connect_to_daemon(self) {
  204. #ifdef USING_OAF
  205.         OAF_ServerInfoList *list;
  206.         CORBA_Environment *ev;
  207.         
  208.         ev = &(self->ev);
  209.         list = oaf_query ("repo_ids.has ('IDL:GNOME/Pilot/Daemon:1.0')", NULL, ev);
  210.         if (list->_length == 0) {
  211.             return GPILOTD_ERR_NOT_CONNECTED;
  212.         } else {
  213.             OAF_ServerInfo info;
  214.             info = list->_buffer[0];
  215.             g_message ("Activating OAF object %s", info.iid);
  216.             self->gpilotddaemon = oaf_activate_from_id (info.iid,           
  217.                                     OAF_FLAG_NO_LOCAL, NULL, NULL);
  218.         }
  219.         CORBA_free (list);
  220.         CORBA_exception_free (ev);
  221. #else /* USING_OAF */
  222.         self->gpilotddaemon = goad_server_activate_with_repo_id(NULL,
  223.                                  "IDL:GNOME/Pilot/Daemon:1.0",
  224.                                       0,NULL);
  225. #endif /* USING_OAF */
  226.         if (self->gpilotddaemon == NULL) return GPILOTD_ERR_NOT_CONNECTED;
  227.  
  228.         return GPILOTD_OK;
  229.     }
  230.  
  231.     private CORBA_char*
  232.     corba_get_client_id(PortableServer_Servant servant,
  233.                 CORBA_Environment *ev) {
  234.         gchar *id;
  235.         id = get_self(servant)->client_id;
  236.         if (id==NULL) return CORBA_string_dup("");
  237.         return CORBA_string_dup(id);
  238.     }
  239.     
  240.     private void
  241.     corba_set_client_id(PortableServer_Servant servant,
  242.                 const CORBA_char *id,
  243.                 CORBA_Environment *ev) {
  244.         if (get_self(servant)->client_id!=NULL) {
  245.             /* throw exception */
  246.                 g_assert(0);
  247.         } get_self(servant)->client_id = g_strdup(id);
  248.     }
  249.  
  250.     private void
  251.     corba_connect(PortableServer_Servant servant,
  252.               const CORBA_char *pilot_id,
  253.               const GNOME_Pilot_PilotUser *user,
  254.               CORBA_Environment *ev) {
  255.         pilot_connect(get_self(servant),pilot_id,user);
  256.     }
  257.  
  258.     private void
  259.     corba_disconnect(PortableServer_Servant servant,
  260.              const CORBA_char *pilot_id,
  261.              CORBA_Environment *ev) {
  262.         pilot_disconnect(get_self(servant),pilot_id);
  263.     }
  264.  
  265.     private void
  266.     corba_request_completed(PortableServer_Servant servant,
  267.                 const CORBA_char *pilot_id,                
  268.                 CORBA_unsigned_long id,
  269.                 CORBA_Environment *ev) {
  270.         completed_request(get_self(servant),pilot_id,id);
  271.     }
  272.  
  273.     private void
  274.     corba_user_info_requested(PortableServer_Servant servant,
  275.                   const CORBA_char *pilot_id,
  276.                   const GNOME_Pilot_PilotUser *user,
  277.                   CORBA_Environment *ev) {
  278.         user_info(get_self(servant),pilot_id,user);
  279.     }
  280.  
  281.     private void
  282.     corba_system_info_requested(PortableServer_Servant servant,
  283.                     const CORBA_char *device,                
  284.                     const GNOME_Pilot_SysInfo *sysinfo,
  285.                     CORBA_Environment *ev) {
  286.         system_info(get_self(servant),device,sysinfo);
  287.     }
  288.  
  289.     private void
  290.     corba_conduit_start(PortableServer_Servant servant,
  291.                 const CORBA_char *pilot_id,
  292.                 const CORBA_char *conduit_name,
  293.                 const CORBA_char *database,
  294.                 CORBA_Environment *ev) {
  295.         start_conduit(get_self(servant),pilot_id,conduit_name,database);
  296.     }
  297.                 
  298.     private void
  299.     corba_conduit_end(PortableServer_Servant servant,
  300.               const CORBA_char *pilot_id,
  301.               const CORBA_char *conduit_name,
  302.               CORBA_Environment *ev) {
  303.         end_conduit(get_self(servant),pilot_id,conduit_name);
  304.     }
  305.                 
  306.     private void
  307.     corba_conduit_progress(PortableServer_Servant servant,
  308.                    const CORBA_char *pilot_id,
  309.                    const CORBA_char *conduit_name,
  310.                    const CORBA_unsigned_long current,
  311.                    const CORBA_unsigned_long total,
  312.                    CORBA_Environment *ev) {
  313.         progress_conduit(get_self(servant),pilot_id,conduit_name,current,total);
  314.     }
  315.                 
  316.     private void
  317.     corba_conduit_message(PortableServer_Servant servant,
  318.                   const CORBA_char *pilot_id,
  319.                   const CORBA_char *conduit_name,
  320.                   const CORBA_char *message,
  321.                   CORBA_Environment *ev) {
  322.         message_conduit(get_self(servant),pilot_id,conduit_name,message);
  323.     }    
  324.         
  325.     private void
  326.     corba_conduit_error(PortableServer_Servant servant,
  327.                 const CORBA_char *pilot_id,
  328.                 const CORBA_char *conduit_name,
  329.                 const CORBA_char *message,
  330.                 CORBA_Environment *ev) {
  331.         error_conduit(get_self(servant),pilot_id,conduit_name,message);
  332.     }
  333.     
  334.     signal first NONE(POINTER,POINTER)
  335.     void pilot_connect(self, const gchar *pilot_name, const GNOME_Pilot_PilotUser *userinfo) {
  336.         return;
  337.     }
  338.  
  339.     signal first NONE(POINTER)
  340.     void pilot_disconnect(self,const gchar *pilot_name) {
  341.         return;
  342.     }
  343.  
  344.     signal first NONE(POINTER,INT)
  345.     void completed_request(self, const gchar *pilot_name, int request_id) {
  346.         return;
  347.     }
  348.  
  349.     signal first NONE(POINTER,POINTER)
  350.     void user_info(self, const gchar *device, const GNOME_Pilot_PilotUser *userinfo) {
  351.         return;
  352.     }
  353.  
  354.     signal first NONE(POINTER,POINTER)
  355.     void system_info(self, const gchar *device, const GNOME_Pilot_SysInfo *sysinfo) {
  356.         return;
  357.     }
  358.  
  359.     signal first NONE(POINTER,POINTER,POINTER)
  360.     void start_conduit(self, const gchar *pilot_name, const gchar *conduit_name, const gchar *database) {
  361.         return;
  362.     }
  363.  
  364.     signal first NONE(POINTER,POINTER)
  365.     void end_conduit(self, const gchar *pilot_name, const gchar *conduit_name) {
  366.         return;
  367.     }
  368.  
  369.     signal first NONE(POINTER,POINTER,UINT,UINT)
  370.     void progress_conduit(self, const gchar *pilot_name, const gchar *conduit_name, guint current, guint total) {
  371.         return;
  372.     }
  373.  
  374.     signal first NONE(POINTER,POINTER,POINTER)
  375.     void message_conduit(self, const gchar *pilot_name, const gchar *conduit_name, const gchar *message) {
  376.         return;
  377.     }
  378.  
  379.     signal first NONE(POINTER,POINTER,POINTER)
  380.     void error_conduit(self, const gchar *pilot_name, const gchar *conduit_name, const gchar *message) {
  381.         return;
  382.     }
  383.  
  384.         public gint pause_daemon(self) onerror GPILOTD_ERR_INVAL {
  385.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  386.  
  387.         GNOME_Pilot_Daemon_pause(self->gpilotddaemon,TRUE,&self->ev);
  388.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  389.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  390.             CORBA_exception_free(&self->ev);
  391.             return GPILOTD_ERR_FAILED;
  392.         }
  393.         return GPILOTD_OK;
  394.     }
  395.  
  396.         public gint unpause_daemon(self) onerror GPILOTD_ERR_INVAL {
  397.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  398.  
  399.         GNOME_Pilot_Daemon_pause(self->gpilotddaemon,FALSE,&self->ev);
  400.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  401.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  402.             CORBA_exception_free(&self->ev);
  403.             return GPILOTD_ERR_FAILED;
  404.         }
  405.         return GPILOTD_OK;
  406.         }
  407.  
  408.         public gint restart_daemon(self) onerror GPILOTD_ERR_INVAL {
  409.         pid_t pid;
  410.         
  411.         /* NOTE: this is going to suck when gpilotd is running on another machine... */
  412.         pid = gpilotd_get_pid();
  413.         if (pid == -1) return -1;
  414.         
  415.         kill(pid,SIGTERM);
  416.         return connect_to_daemon(self);
  417.         }
  418.  
  419.     public gint monitor_on(self,
  420.                    const gchar *pilot_id (check null)) onerror GPILOTD_ERR_INVAL {
  421.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  422.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  423.         GNOME_Pilot_Daemon_monitor_on(self->gpilotddaemon,self->gpilotdclient,pilot_id,&self->ev);
  424.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  425.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  426.             CORBA_exception_free(&self->ev);
  427.             return GPILOTD_ERR_FAILED;
  428.         }
  429.         return GPILOTD_OK;
  430.     }    
  431.  
  432.         public gint monitor_on_all_pilots(self) {
  433.         GList   *pilots = NULL,
  434.             *ptr;
  435.         gint ret = GPILOTD_OK;
  436.         
  437.         if (get_pilots(self,&pilots) != GPILOTD_OK) return GPILOTD_ERR_FAILED;
  438.         
  439.         for (ptr = pilots; ptr; ptr = ptr->next) {
  440.             ret = monitor_on(self,(gchar*)ptr->data);
  441.         }
  442.         g_list_foreach(pilots,(GFunc)g_free,NULL);
  443.         return ret;
  444.         }
  445.  
  446.     public gint monitor_off(self,
  447.                 const gchar *pilot_id (check null)) onerror GPILOTD_ERR_INVAL {
  448.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  449.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  450.  
  451.         GNOME_Pilot_Daemon_monitor_off(self->gpilotddaemon,self->gpilotdclient,pilot_id,&self->ev);
  452.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  453.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  454.             CORBA_exception_free(&self->ev);
  455.             return GPILOTD_ERR_FAILED;
  456.         }
  457.         return GPILOTD_OK;
  458.     }    
  459.  
  460.         public gint monitor_off_all_pilots(self) {
  461.         GList   *pilots = NULL,
  462.             *ptr;
  463.         gint ret = GPILOTD_OK;
  464.         
  465.         if (get_pilots(self,&pilots) != GPILOTD_OK) return GPILOTD_ERR_FAILED;
  466.         
  467.         for (ptr = pilots; ptr; ptr = ptr->next) {
  468.             ret = monitor_on(self,(gchar*)ptr->data);
  469.         }
  470.         g_list_foreach(pilots,(GFunc)g_free,NULL);
  471.         return ret;
  472.     }
  473.  
  474.     public gint notify_on(self,
  475.                   GNOME_Pilot_EventType type) {
  476.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  477.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  478.  
  479.         GNOME_Pilot_Daemon_notify_on(self->gpilotddaemon,type,self->gpilotdclient,&self->ev);
  480.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  481.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  482.             CORBA_exception_free(&self->ev);
  483.             return GPILOTD_ERR_FAILED;
  484.         }
  485.         return GPILOTD_OK;
  486.     }    
  487.  
  488.     public gint notify_off(self,
  489.                    GNOME_Pilot_EventType type) onerror GPILOTD_ERR_INVAL {
  490.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  491.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  492.  
  493.         GNOME_Pilot_Daemon_notify_off(self->gpilotddaemon,type,self->gpilotdclient,&self->ev);
  494.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  495.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  496.             CORBA_exception_free(&self->ev);
  497.             return GPILOTD_ERR_FAILED;
  498.         }
  499.         return GPILOTD_OK;
  500.     }    
  501.  
  502.     public gint restore(self,
  503.                 const gchar *pilot_id (check null),
  504.                 const gchar *directory (check null),
  505.                 GNOME_Pilot_Survival survival,
  506.                 gint timeout (check >= 0),
  507.                 gint *handle) onerror GPILOTD_ERR_INVAL {
  508.         gint val;
  509.  
  510.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  511.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  512.  
  513.         val = GNOME_Pilot_Daemon_request_restore(self->gpilotddaemon,
  514.                              self->gpilotdclient,
  515.                              pilot_id,
  516.                              directory,
  517.                              survival,
  518.                              timeout,
  519.                              &self->ev);
  520.         switch(self->ev._major) {
  521.         case CORBA_SYSTEM_EXCEPTION:
  522.         case CORBA_USER_EXCEPTION: {
  523.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  524.             if (strcmp(ex_GNOME_Pilot_MissingFile,CORBA_exception_id(&self->ev))==0) {
  525.                 g_warning("Missing file");
  526.             } 
  527.             CORBA_exception_free(&self->ev);
  528.             return GPILOTD_ERR_FAILED;
  529.             break;
  530.         }
  531.         case CORBA_NO_EXCEPTION: 
  532.         default:
  533.             break;
  534.         }
  535.  
  536.         if (handle!=NULL) *handle = val;
  537.  
  538.         return GPILOTD_OK;
  539.     }        
  540.  
  541.     public gint install_file(self,
  542.                  const gchar *pilot_name (check null),
  543.                  const gchar *infile (check null),
  544.                  GNOME_Pilot_Survival survival,
  545.                  gint timeout (check >=0),
  546.                  gint *handle) onerror GPILOTD_ERR_INVAL {
  547.         gchar *filename;
  548.         gchar *queue;
  549.         FILE *in,*out;
  550.         gint val;
  551.  
  552.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  553.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  554.  
  555.         queue = g_strdup_printf("%s/.gnome/gnome-pilot.d/",g_get_home_dir());
  556.         filename = tempnam(queue,"PDB");
  557.         
  558.         in = fopen(infile,"rb");
  559.         if (in==NULL) return GPILOTD_ERR_FAILED;
  560.         out = fopen(filename,"wb");
  561.         if (out==NULL) return GPILOTD_ERR_FAILED;
  562.  
  563.         while(!feof(in)) {
  564.             size_t act;
  565.             char block[1024];
  566.             act=fread(block,1,1024,in);
  567.             if (ferror(in)) return GPILOTD_ERR_FAILED;
  568.             if (fwrite(block,1,act,out)!=act) return GPILOTD_ERR_FAILED;
  569.             if (ferror(out)) return GPILOTD_ERR_FAILED;
  570.         }
  571.         
  572.         fflush(out);
  573.         fclose(in); 
  574.         fclose(out);        
  575.  
  576.         val = GNOME_Pilot_Daemon_request_install(self->gpilotddaemon,
  577.                              self->gpilotdclient,
  578.                              pilot_name,
  579.                              filename,
  580.                              infile,
  581.                              survival,
  582.                              timeout,
  583.                              &self->ev);
  584.         switch(self->ev._major) {
  585.         case CORBA_NO_EXCEPTION: 
  586.             break;
  587.         case CORBA_SYSTEM_EXCEPTION:
  588.         case CORBA_USER_EXCEPTION:
  589.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  590.             
  591.             if (strcmp(ex_GNOME_Pilot_MissingFile,CORBA_exception_id(&self->ev))==0) {
  592.                 g_warning("Missing file");
  593.             } else {
  594.                 unlink(filename);
  595.             }
  596.             CORBA_exception_free(&self->ev);
  597.             return GPILOTD_ERR_FAILED;
  598.             break;
  599.         default:
  600.             break;
  601.         }
  602.  
  603.         if (handle!=NULL) *handle = val;
  604.  
  605.         return GPILOTD_OK;
  606.     }
  607.  
  608.         public gint get_user_info(self,
  609.                   gchar *cradle_name (check null),
  610.                   GNOME_Pilot_Survival survival,
  611.                   gint timeout (check >= 0),
  612.                               gint *handle) onerror GPILOTD_ERR_INVAL {
  613.         gint val;
  614.  
  615.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  616.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  617.  
  618.         val = GNOME_Pilot_Daemon_get_user_info(self->gpilotddaemon,
  619.                                self->gpilotdclient,
  620.                                cradle_name,
  621.                                survival,
  622.                                timeout,
  623.                                &self->ev);
  624.  
  625.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  626.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  627.             CORBA_exception_free(&self->ev);
  628.             return GPILOTD_ERR_FAILED;
  629.         }
  630.  
  631.         if (handle!=NULL) *handle =val;
  632.  
  633.         return GPILOTD_OK;
  634.     }
  635.  
  636.         public gint set_user_info(self,
  637.                   gchar *cradle_name (check null),
  638.                   GNOME_Pilot_PilotUser user,
  639.                   gboolean continue_sync,
  640.                   GNOME_Pilot_Survival survival,
  641.                   gint timeout (check >= 0),
  642.                   gint *handle) onerror GPILOTD_ERR_INVAL {
  643.         gint val;
  644.  
  645.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  646.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  647.  
  648.         val = GNOME_Pilot_Daemon_set_user_info(self->gpilotddaemon,
  649.                                self->gpilotdclient,
  650.                                &user,
  651.                                cradle_name,
  652.                                continue_sync,
  653.                                survival,
  654.                                timeout,
  655.                                &self->ev);
  656.  
  657.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  658.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  659.             CORBA_exception_free(&self->ev);
  660.             return GPILOTD_ERR_FAILED;
  661.         }
  662.  
  663.         if (handle!=NULL) *handle =val;
  664.  
  665.         return GPILOTD_OK;
  666.     }
  667.               
  668.         public gint get_system_info(self,
  669.                     gchar *cradle_name (check null),
  670.                     GNOME_Pilot_Survival survival,
  671.                     gint timeout) {
  672. #if __GNUC__
  673. #warning get_system_info not implemented
  674. #endif
  675.         return 0;
  676.     }
  677.  
  678.         public gint remove_request(self,
  679.                    gint handle) onerror GPILOTD_ERR_INVAL {
  680.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  681.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  682.  
  683.         GNOME_Pilot_Daemon_remove_request(self->gpilotddaemon,handle,&self->ev);
  684.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  685.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  686.             CORBA_exception_free(&self->ev);
  687.             return GPILOTD_ERR_FAILED;
  688.         }
  689.         return GPILOTD_OK;        
  690.     }
  691.  
  692.         public gint conduit(self,
  693.                 const gchar *pilot_name (check null),
  694.                 const gchar *conduit_name (check null),
  695.                 GNOME_Pilot_ConduitOperation operation,
  696.                 GNOME_Pilot_Survival survival, 
  697.                 gint timeout (check >= 0),
  698.                 gint *handle) onerror GPILOTD_ERR_INVAL {
  699.         gint val;
  700.  
  701.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  702.         g_return_val_if_fail(self->gpilotdclient!=NULL, GPILOTD_ERR_INTERNAL);
  703.         
  704.         val = GNOME_Pilot_Daemon_request_conduit(self->gpilotddaemon,
  705.                              self->gpilotdclient,
  706.                              pilot_name,
  707.                              conduit_name,
  708.                              operation,
  709.                              survival,
  710.                              timeout,
  711.                              &self->ev);
  712.         
  713.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  714.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  715.             CORBA_exception_free(&self->ev);
  716.             return GPILOTD_ERR_FAILED;
  717.         }
  718.  
  719.         if (handle!=NULL) *handle =val;
  720.  
  721.         return GPILOTD_OK;
  722.     }
  723.  
  724.     private gint get_triple_ptr(self,
  725.                     enum get_triple_ptr_action action,
  726.                     const gchar *name,
  727.                     GList **output (check null)) onerror GPILOTD_ERR_INVAL {
  728.         GNOME_Pilot_StringSequence *seq;
  729.     
  730. #ifndef G_DISABLE_CHECKS        
  731.         if (*output!=NULL) {
  732.             g_warning("%s:%d: get_triple_ptr called with non-null pointer for output, leak-alert!",
  733.                   __FILE__,__LINE__);
  734.         }
  735. #endif
  736.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  737.  
  738.         (*output) = NULL;
  739.  
  740.         switch(action) {
  741.         case GPILOTD_APP_PILOT_NAMES:
  742.             seq = GNOME_Pilot_Daemon_get_pilots(self->gpilotddaemon, &self->ev);
  743.             break;
  744.         case GPILOTD_APP_PILOTS_BY_NAME:
  745.             seq = GNOME_Pilot_Daemon_get_pilots_by_user_name(self->gpilotddaemon,name,&self->ev);
  746.             break;
  747.         case GPILOTD_APP_PILOTS_BY_LOGIN:
  748.             seq = GNOME_Pilot_Daemon_get_pilots_by_user_login(self->gpilotddaemon,name,&self->ev);
  749.             break;
  750.         case GPILOTD_APP_USER_NAMES:
  751.             seq = GNOME_Pilot_Daemon_get_users(self->gpilotddaemon,&self->ev);
  752.             break;
  753.         case GPILOTD_APP_DATABASES_FROM_CACHE:
  754.             seq = GNOME_Pilot_Daemon_get_databases_from_cache(self->gpilotddaemon,name,&self->ev);
  755.             break;
  756.         case GPILOTD_APP_CRADLES:
  757.             seq = GNOME_Pilot_Daemon_get_cradles(self->gpilotddaemon,&self->ev);
  758.             break;
  759.         }
  760.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  761.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  762.             CORBA_exception_free(&self->ev);
  763.             return GPILOTD_ERR_FAILED;
  764.         }
  765.         if (seq->_length>0) {
  766.             int i;
  767.             for(i=0;i<seq->_length;i++) {
  768.                 if (strlen(seq->_buffer[i]))
  769.                     (*output) = g_list_append((*output),g_strdup(seq->_buffer[i]));
  770.             }
  771.         } 
  772.         
  773.         CORBA_free(seq);
  774.  
  775.         return GPILOTD_OK;
  776.     }
  777.  
  778.         public gint get_users(self,
  779.                   GList **output (check null)) onerror GPILOTD_ERR_INVAL {
  780.         return get_triple_ptr(self,GPILOTD_APP_USER_NAMES,NULL,output);
  781.     }
  782.  
  783.         public gint get_databases_from_cache(self,
  784.                          const gchar *pilot_name,
  785.                          GList **output (check null)) onerror GPILOTD_ERR_INVAL {
  786.         return get_triple_ptr(self,GPILOTD_APP_DATABASES_FROM_CACHE,pilot_name,output);
  787.     }
  788.  
  789.     public gint get_cradles(self,
  790.                 GList **output (check null)) onerror GPILOTD_ERR_INVAL {
  791.         return get_triple_ptr(self,GPILOTD_APP_CRADLES,NULL,output);
  792.     }
  793.  
  794.     public gint get_pilots(self,
  795.                     GList **output (check null)) onerror GPILOTD_ERR_INVAL {
  796.         return get_triple_ptr(self,GPILOTD_APP_PILOT_NAMES,NULL,output);
  797.     }
  798.  
  799.     public gint get_pilot_ids(self,
  800.                   gint **output (check null)) onerror GPILOTD_ERR_INVAL {
  801. #if __GNUC__
  802. #warning get_pilot_ids not implemented
  803. #endif
  804.         return 0;
  805.     }
  806.  
  807.  
  808.     public gint get_pilots_by_user_name(self,
  809.                         const gchar *name (check null),
  810.                         GList **output (check null)) onerror GPILOTD_ERR_INVAL {
  811.         return get_triple_ptr(self,GPILOTD_APP_PILOTS_BY_NAME,name,output);
  812.     }
  813.  
  814.     public gint get_pilots_by_user_login(self,
  815.                          const gchar *login (check null),
  816.                          GList **output (check null)) onerror GPILOTD_ERR_INVAL {
  817.         return get_triple_ptr(self,GPILOTD_APP_PILOTS_BY_LOGIN,login,output);
  818.     }
  819.  
  820.     public gint get_user_name_by_pilot_name(self,
  821.                         const gchar *pilot_name (check null),
  822.                         gchar **output (check null)) onerror GPILOTD_ERR_INVAL {
  823. #if __GNUC__
  824. #warning get_user_name_by_pilot_name not implemented
  825. #endif
  826.         return 0;
  827.     }
  828.  
  829.         public gint get_user_login_by_pilot_name(self,
  830.                          const gchar *pilot_name (check null),
  831.                          gchar **output (check null)) onerror GPILOTD_ERR_INVAL {
  832. #if __GNUC__
  833. #warning get_user_login_by_pilot_name not implemented
  834. #endif
  835.         return 0;
  836.     }
  837.  
  838.     /* Output must be a pointer to a gchar*, and *output should be
  839.        NULL when called. The pointer returned in *output must be
  840.        g_free'd by the caller. *output will be NULL if anything by
  841.        GPILOTD_OK is returned */
  842.     public gint get_pilot_base_dir_by_name(self,
  843.                         const gchar *pilot_name (check null),
  844.                         gchar **output (check null)) onerror GPILOTD_ERR_INVAL {
  845.         CORBA_char *basedir;
  846.  
  847.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  848. #ifndef G_DISABLE_CHECKS        
  849.         if (*output!=NULL) {
  850.             g_warning("%s:%d: get_pilot_base_dir_by_name called with non-null pointer for output, leak-alert!",
  851.                   __FILE__,__LINE__);
  852.         }
  853. #endif
  854.         basedir = GNOME_Pilot_Daemon_get_pilot_base_dir(self->gpilotddaemon,pilot_name,&self->ev);
  855.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  856.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  857.             CORBA_exception_free(&self->ev);
  858.             return GPILOTD_ERR_FAILED;
  859.         }
  860.         
  861.         *output = g_strdup(basedir);
  862.         CORBA_free(basedir);
  863.         
  864.         if (strlen(*output)==0) {
  865.             g_free(*output);
  866.                *output = g_strdup_printf("%s/%s",g_get_home_dir(),pilot_name);
  867.         }
  868.         
  869.         return GPILOTD_OK;    
  870.  
  871.     }
  872.  
  873.     public gint get_pilot_base_dir_by_id(self,
  874.                          guint32 pilot_id,
  875.                          gchar **output (check null)) onerror GPILOTD_ERR_INVAL {
  876.         gchar *pilot_name;
  877.         gint return_code;
  878.  
  879.         return_code = get_pilot_name_by_id(self,pilot_id,&pilot_name);
  880.         if (return_code!=GPILOTD_OK) {
  881.             return return_code;
  882.         }
  883.  
  884.         return_code = get_pilot_base_dir_by_name(self,pilot_name,output);
  885.         g_free(pilot_name);
  886.         return return_code;
  887.     }
  888.  
  889.  
  890.     public gint get_pilot_id_by_name(self,
  891.                        const gchar *pilot_name (check null),
  892.                        guint32 *output (check null)) onerror GPILOTD_ERR_INVAL {        
  893.         g_return_val_if_fail(self->gpilotddaemon!=NULL, GPILOTD_ERR_NOT_CONNECTED);
  894.         
  895.         *output = GNOME_Pilot_Daemon_get_pilot_id_from_name(self->gpilotddaemon,pilot_name,&self->ev);
  896.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  897.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  898.             CORBA_exception_free(&self->ev);
  899.             return GPILOTD_ERR_FAILED;
  900.         }
  901.         
  902.         return GPILOTD_OK;    
  903.     }
  904.  
  905.     public gint get_pilot_name_by_id(self,
  906.                      guint32 pilot_id,
  907.                      gchar **output (check null)) onerror GPILOTD_ERR_INVAL {
  908.         CORBA_char *name;
  909. #ifndef G_DISABLE_CHECKS        
  910.         if (*output!=NULL) {
  911.             g_warning("%s:%d: get_pilot_name_by_id called with non-null pointer for output, leak-alert!",
  912.                   __FILE__,__LINE__);
  913.         }
  914. #endif
  915.         name = GNOME_Pilot_Daemon_get_pilot_name_from_id(self->gpilotddaemon,pilot_id,&self->ev);
  916.         if (self->ev._major != CORBA_NO_EXCEPTION) {
  917.             g_warning ("%s:%d: Caught exception: %s",__FILE__,__LINE__, CORBA_exception_id (&self->ev));
  918.             CORBA_exception_free(&self->ev);
  919.             return GPILOTD_ERR_FAILED;
  920.         }
  921.         
  922.         *output = g_strdup(name);
  923.         CORBA_free(name);
  924.         
  925.         if (output==NULL || strlen(*output)==0) {
  926.             g_free(*output);
  927.             return GPILOTD_ERR_FAILED;
  928.         }
  929.         
  930.         return GPILOTD_OK;    
  931.     }
  932.  
  933.  
  934.  
  935. }
  936.  
  937. %{
  938.  
  939. pid_t
  940. gpilotd_get_pid(void)
  941. {
  942.     FILE *f;
  943.     gchar *homedir;
  944.     GString *str;
  945.     gchar pidstr[100];
  946.     pid_t pid;
  947.     
  948.     homedir = g_get_home_dir(); 
  949.     if (!homedir)
  950.         return -1;
  951.     
  952.     str = g_string_new(homedir);
  953.     g_string_append(str, "/.gpilotd.pid");
  954.     f = fopen(str->str, "r");
  955.     g_string_free(str, TRUE);
  956.     if (!f) {
  957.         return -1;
  958.     } else {
  959.         fgets(pidstr, sizeof(pidstr), f);
  960.         fclose(f);
  961.         pid = strtol(pidstr, NULL, 10);
  962.         if (pid == '\0')
  963.             return -1;
  964.         else
  965.             return pid;
  966.     }
  967. }
  968.  
  969. GnomePilotClient *
  970. get_self(PortableServer_Servant servant) {
  971.     GnomePilotClientServant *s;
  972.     s = (GnomePilotClientServant*)servant;
  973.     return s->self;
  974. }
  975.  
  976. %}
  977.